package com.java.shop.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.exception.BizCodeEnum;
import com.java.common.exception.MyException;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.shop.member.dao.MemberDao;
import com.java.shop.member.entity.MemberEntity;
import com.java.shop.member.service.MemberService;
import com.java.shop.member.vo.LoginVo;
import com.java.shop.member.vo.RegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Slf4j
@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增会员
     * @param vo
     * @return
     */
    @Override
    public Boolean regist(RegistVo vo) throws MyException{
        Boolean hasUsername = hasUsernameOrPhone(vo.getUsername(), vo.getPhone());
        if(hasUsername){
            throw new MyException(3500,"用户名或手机号已存在，请更换");
        }
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername(vo.getUsername());
        memberEntity.setMobile(vo.getPhone());
        String password = new BCryptPasswordEncoder().encode(vo.getPassword());
        memberEntity.setPassword(password);
        return this.save(memberEntity);
    }

    /**
     * 判断用户名或手机号是否已存在（规定：用户名或手机号唯一）
     * @param username
     * @param phone
     * @return
     */
    public Boolean hasUsernameOrPhone(String username, String phone){
        int nicknameCount = this.count(new QueryWrapper<MemberEntity>().eq("nickname", username));
        if(nicknameCount > 0) return true;
        int mobileCount = this.count(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(mobileCount > 0) return true;
        return false;
    }

    /**
     * 根据账户返回对应的member信息
     * @param vo
     * @return
     */
    @Override
    public MemberEntity loginByAccount(LoginVo vo) throws MyException{
        MemberEntity entity = this.getOne(new QueryWrapper<MemberEntity>().eq("username", vo.getAccount()).or().eq("mobile", vo.getAccount()));
        if(entity == null){
            log.error("MemberServiceImpl::loginByAccount查询会员信息为空");
            throw new MyException(BizCodeEnum.ACCOUNT_OR_PASSWORD_ERROR.getCode(),BizCodeEnum.ACCOUNT_OR_PASSWORD_ERROR.getMessage());
        }
        String password = entity.getPassword();
        boolean matches = new BCryptPasswordEncoder().matches(vo.getPassword(), password);
        if(!matches){
            log.error("MemberServiceImpl::loginByAccount密码错误");
            throw new MyException(BizCodeEnum.ACCOUNT_OR_PASSWORD_ERROR.getCode(),BizCodeEnum.ACCOUNT_OR_PASSWORD_ERROR.getMessage());
        }
        //TODO 创建session
        return entity;
    }
}